package com.example.springboot_sdse_air.controller;

import com.example.springboot_sdse_air.entity.User;
import com.example.springboot_sdse_air.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
@CrossOrigin(origins = "http://localhost:2525", allowCredentials = "true")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Map<String, Object> register(@RequestParam String username, 
                                       @RequestParam String password) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            if (username == null || username.trim().isEmpty()) {
                result.put("success", false);
                result.put("message", "用户名不能为空");
                return result;
            }
            
            if (password == null || password.trim().isEmpty()) {
                result.put("success", false);
                result.put("message", "密码不能为空");
                return result;
            }
            
            boolean success = userService.register(username, password);
            if (success) {
                result.put("success", true);
                result.put("message", "注册成功");
            } else {
                result.put("success", false);
                result.put("message", "用户名已存在");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "注册失败：" + e.getMessage());
        }
        
        return result;
    }
    
    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Map<String, Object> login(@RequestParam String username, 
                                    @RequestParam String password,
                                    HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            if (username == null || username.trim().isEmpty()) {
                result.put("success", false);
                result.put("message", "用户名不能为空");
                return result;
            }
            
            if (password == null || password.trim().isEmpty()) {
                result.put("success", false);
                result.put("message", "密码不能为空");
                return result;
            }
            
            User user = userService.login(username, password);
            if (user != null) {
                // 将用户信息存储到session中
                session.setAttribute("user", user);
                result.put("success", true);
                result.put("message", "登录成功");
                result.put("user", user);
            } else {
                result.put("success", false);
                result.put("message", "用户名或密码错误");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "登录失败：" + e.getMessage());
        }
        
        return result;
    }
    
    /**
     * 用户登出
     */
    @PostMapping("/logout")
    public Map<String, Object> logout(HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            session.removeAttribute("user");
            result.put("success", true);
            result.put("message", "登出成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "登出失败：" + e.getMessage());
        }
        
        return result;
    }
    
    /**
     * 获取当前登录用户信息
     */
    @GetMapping("/current")
    public Map<String, Object> getCurrentUser(HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        
        User user = (User) session.getAttribute("user");
        if (user != null) {
            result.put("success", true);
            result.put("user", user);
        } else {
            result.put("success", false);
            result.put("message", "用户未登录");
        }
        
        return result;
    }
    
    /**
     * 检查用户名是否存在
     */
    @GetMapping("/check-username")
    public Map<String, Object> checkUsername(@RequestParam String username) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            boolean exists = userService.isUsernameExists(username);
            result.put("success", true);
            result.put("exists", exists);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "检查失败：" + e.getMessage());
        }
        
        return result;
    }
} 